﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_UserLevel.cs
* 创建标识：曾乃强 15158756652 2011-03-17 11:03:01
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;


namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_UserLevel*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_UserLevel
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_UserLevel obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "UserLevel ([LevelName],[DisCountPercent],[IsAcquiesce],[NeedPointsNum],[IsDelete])");
            sql.Append("values(@LevelName,@DisCountPercent,@IsAcquiesce,@NeedPointsNum,@IsDelete)");
            
            SqlParameter[] pra = {
                            new SqlParameter("@LevelName",obj.LevelName),
							new SqlParameter("@DisCountPercent",obj.DisCountPercent),
							new SqlParameter("@IsAcquiesce",obj.IsAcquiesce),
							new SqlParameter("@NeedPointsNum",obj.NeedPointsNum),
							new SqlParameter("@IsDelete",obj.IsDelete)
                                 };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Update(Model.CNVP_UserLevel obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "UserLevel set [LevelName] = @LevelName,[DisCountPercent] = @DisCountPercent,[IsAcquiesce] = @IsAcquiesce,[NeedPointsNum] = @NeedPointsNum where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@LevelName",obj.LevelName),
							new SqlParameter("@DisCountPercent",obj.DisCountPercent),
							new SqlParameter("@IsAcquiesce",obj.IsAcquiesce),
							new SqlParameter("@NeedPointsNum",obj.NeedPointsNum)
                                };
             return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public int Delete(Model.CNVP_UserLevel obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "UserLevel where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public List<Model.CNVP_UserLevel> LoadAll()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[LevelName],[DisCountPercent],[IsAcquiesce],[NeedPointsNum],[IsDelete] from " + Prefix + "UserLevel");
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_UserLevel> list=new List<Model.CNVP_UserLevel>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_UserLevel cnvp_userlevel = new Model.CNVP_UserLevel();
                    cnvp_userlevel.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_userlevel.LevelName = dr["LevelName"] is DBNull ? String.Empty : (string)dr["LevelName"];
                    cnvp_userlevel.DisCountPercent = dr["DisCountPercent"] is DBNull ? (decimal)0.00 : (decimal)dr["DisCountPercent"];
                    cnvp_userlevel.IsAcquiesce = dr["IsAcquiesce"] is DBNull ? (byte)0 : (byte)dr["IsAcquiesce"];
                    cnvp_userlevel.NeedPointsNum = dr["NeedPointsNum"] is DBNull ? 0 : (int)dr["NeedPointsNum"];
                    cnvp_userlevel.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
                    list.Add(cnvp_userlevel);
                }
            }
            return list;

        }

        public Model.CNVP_UserLevel LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [LevelName],[DisCountPercent],[IsAcquiesce],[NeedPointsNum],[IsDelete] from " + Prefix + "UserLevel");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            Model.CNVP_UserLevel cnvp_userlevel = new Model.CNVP_UserLevel();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_userlevel.LevelName = dr["LevelName"] is DBNull ? String.Empty : (string)dr["LevelName"];
                cnvp_userlevel.DisCountPercent = dr["DisCountPercent"] is DBNull ? (decimal)0.00 : (decimal)dr["DisCountPercent"];
                cnvp_userlevel.IsAcquiesce = dr["IsAcquiesce"] is DBNull ? (byte)0 : (byte)dr["IsAcquiesce"];
                cnvp_userlevel.NeedPointsNum = dr["NeedPointsNum"] is DBNull ? 0 : (int)dr["NeedPointsNum"];
                cnvp_userlevel.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
            }
            return cnvp_userlevel;
        }

        #endregion

        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string AllFields = "ID, LevelName, DisCountPercent, IsAcquiesce, NeedPointsNum, IsDelete";
                string Condition = " " + Prefix + "UserLevel";
                string IndexField = "ID";
                string OrderFields = "order by Id Desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }

        public List<Model.CNVP_UserLevel> LoadAll(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[LevelName],[DisCountPercent],[IsAcquiesce],[NeedPointsNum],[IsDelete] from " + Prefix + "UserLevel " + sqlWhere + " ");
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_UserLevel> list = new List<Model.CNVP_UserLevel>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_UserLevel cnvp_userlevel = new Model.CNVP_UserLevel();
                    cnvp_userlevel.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_userlevel.LevelName = dr["LevelName"] is DBNull ? String.Empty : (string)dr["LevelName"];
                    cnvp_userlevel.DisCountPercent = dr["DisCountPercent"] is DBNull ? (decimal)0.00 : (decimal)dr["DisCountPercent"];
                    cnvp_userlevel.IsAcquiesce = dr["IsAcquiesce"] is DBNull ? (byte)0 : (byte)dr["IsAcquiesce"];
                    cnvp_userlevel.NeedPointsNum = dr["NeedPointsNum"] is DBNull ? 0 : (int)dr["NeedPointsNum"];
                    cnvp_userlevel.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
                    list.Add(cnvp_userlevel);
                }
            }
            return list;

        }

        public Model.CNVP_UserLevel LoadBysqlWhere(string sqlWhere)
        {

            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[LevelName],[DisCountPercent],[IsAcquiesce],[NeedPointsNum],[IsDelete] from " + Prefix + "UserLevel " + sqlWhere + "");
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            Model.CNVP_UserLevel cnvp_userlevel = new Model.CNVP_UserLevel();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_userlevel.ID = dr["ID"] is DBNull ? (int)0 : (int)dr["ID"];
                cnvp_userlevel.LevelName = dr["LevelName"] is DBNull ? String.Empty : (string)dr["LevelName"];
                cnvp_userlevel.DisCountPercent = dr["DisCountPercent"] is DBNull ? (decimal)0.00 : (decimal)dr["DisCountPercent"];
                cnvp_userlevel.IsAcquiesce = dr["IsAcquiesce"] is DBNull ? (byte)0 : (byte)dr["IsAcquiesce"];
                cnvp_userlevel.NeedPointsNum = dr["NeedPointsNum"] is DBNull ? 0 : (int)dr["NeedPointsNum"];
                cnvp_userlevel.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
            }
            return cnvp_userlevel;
        }

        #region 设置默认时
        /// <summary>
        /// 把为‘1’全部更新设置成‘0’
        /// </summary>
        public int UpdateWhereOne()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "UserLevel set [IsAcquiesce] = 0 where [IsAcquiesce] = 1");
            return DbHelper.ExecuteNonQuery(sql.ToString());
        }
        #endregion
    }
}

